<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="50%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="任务名称" prop="item1">
                <el-input v-model="ruleForm.item1" class="w100p" placeholder="请输入计划名称"></el-input>
            </el-form-item>
            <el-form-item label="巡查路线" prop="item2">
                <el-select v-model="ruleForm.item2" class="w100p" clearable filterable placeholder="请选择路线">
                    <el-option label="路线1" value="路线1"></el-option>
                    <el-option label="路线2" value="路线2"></el-option>
                    <el-option label="路线3" value="路线3"></el-option>
                    <el-option label="路线4" value="路线4"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="巡查人" prop="item3">
                <el-select v-model="ruleForm.item3" class="w100p" clearable filterable placeholder="请选择巡查人">
                    <el-option label="黄小米" value="黄小米"></el-option>
                    <el-option label="小黄米" value="小黄米"></el-option>
                    <el-option label="黄米小" value="黄米小"></el-option>
                    <el-option label="米黄小" value="米黄小"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="任务执行时间" prop="item4">
                <el-date-picker v-model="ruleForm.item4" align="right" type="datetime" placeholder="选择时间">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data () {
        let validatorNumber = function (rule, value, callback) {
            if (value > 0 && value % 1 == 0) {
                callback()
            } else {
                callback(new Error('请输入大于0的整数值！'))
            }
        }
        return {
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                item1: [
                    { required: true, message: '请输入任务名称', trigger: 'change' }
                ],
                item2: [
                    { required: true, message: '请选择路线', trigger: 'change' }
                ],
                item3: [
                    { required: true, message: '请选择巡查人', trigger: 'change' }
                ],
                item4: [
                    { required: true, message: '请选择时间', trigger: 'change' }
                ]
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible (v) {
            v && this.initData()
        }
    },
    
    mounted () { },
    methods: {
        initData () {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                this.addEditApi = ''
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = ''
                this.addEditName = '编辑'
            }
        },
        //提交表单
        submitForm () {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi.then(res => {
                        this.cancel()
                    }).catch(err => {

                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm () {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel () {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose (done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel () })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped></style>
  